package com.kuzan.autocloud.client.sys.config.datainit;

import com.kuzan.autocloud.client.sys.config.Initialize;
import com.kuzan.autocloud.common.SelectItems;
import com.kuzan.autocloud.common.model.organization.Organization;
import com.kuzan.autocloud.common.model.organization.Person;
import com.kuzan.autocloud.common.repository.organization.OrganizationRepository;
import com.kuzan.autocloud.common.repository.organization.PersonRepository;
import com.kuzan.autocloud.common.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * <p>Description: </p>
 *
 * @author kuzank 2019/12/2
 */
@Service
@Order(0)
public class InitOrganizationService implements Initialize {

    public static final String Id_Person_Fhs = "00001";
    public static final String Id_Person_FYT = "00002";

    public static final String Id_Organization_Deleloper = "01001";
    public static final String Id_Organization_Finance = "01002";

    @Autowired
    PersonRepository personRepository;
    @Autowired
    OrganizationRepository organizationRepository;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void initializedb() throws Exception {

        Person personfhs = Person.builder()
                .email("754109648@qq.com")
                .phone("15219378950")
                .gender(SelectItems.Gender.Man.getName())
                .username("kuzank")
                .organization(Id_Organization_Deleloper)
                .password(bCryptPasswordEncoder.encode(MD5Util.MD5("auto-cloud").toLowerCase()))
                .build();
        personfhs.setId(Id_Person_Fhs);
        personfhs.setTitle("方浩生");

        Person personfyt = Person.builder()
                .email("danxiaoguidecade@gmail.com")
                .phone("15219378950")
                .gender(SelectItems.Gender.Man.getName())
                .username("danxiaogui")
                .organization(Id_Organization_Finance)
                .password(bCryptPasswordEncoder.encode(MD5Util.MD5("auto-cloud").toLowerCase()))
                .build();
        personfyt.setId(Id_Person_FYT);
        personfyt.setTitle("方一天");

        Organization organizationDeleloper = Organization.ofRoot("开发人员", Id_Organization_Deleloper, Id_Person_Fhs);
        Organization organizationFinance = Organization.ofRoot("财务部", Id_Organization_Finance, Id_Person_FYT);


        personRepository.saveAll(
                Arrays.asList(personfhs, personfyt)
        );

        organizationRepository.saveAll(
                Arrays.asList(organizationDeleloper, organizationFinance)
        );
    }

}
